<template>
  <div class="result-container">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="(item, index) in list" :key="index" :title="item.title"></van-cell>
    </van-list>
  </div>
</template>

<script>
import { getSearchResult } from '@/api/article.js'
export default {
  name: 'ResultSearch',
  props: {
    searchValue: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      error: false,
      query: {
        page: 1,
        per_page: 20,
        q: ''
      }
    }
  },
  methods: {
    async onLoad() {
      try {
        this.query.q = this.searchValue
        const { data } = await getSearchResult(this.query)
        const articleList = data.data.results
        this.list.push(...articleList)
        // 加载状态结束
        this.loading = false
        // 数据全部加载完成
        if (articleList.length) {
          this.query.page++
        } else {
          this.finished = true
        }
      } catch (error) {
          this.$toast('数据获取失败')
          this.loading = false
      }
    }
  }
}
</script>

<style scope lang="less"></style>
